#include "UserInterface.h"


UserInterface::UserInterface(void)
{
	uis = MENU;
	resume = false;
	pInput = Input::getInstance();
	for(int i=0; i<MAX_MOUSEBUT; ++i)
		mbdown[i] = false;
	pEngine = DX9Engine::getInstance();
}

UserInterface::~UserInterface(void)
{
}

void UserInterface::initialize()
{
	window.initialize(L"images/window.png", V2DF(SCREEN_WIDTH*0.5, SCREEN_HEIGHT*0.5), 0.0f, 1.0f, true);
	border.initialize(L"images/border.png");
	// border thickness
	const int t = 6;
	// initialize buttons
	for(int i=0; i<MENU_BUTCNT; ++i)
	{
		// menu buttons
		menuButton[i].initialize(L"images/button.png", V2DF(SCREEN_WIDTH*0.5, 200+(i*100)),
			0.0f, 1.0f, true);

		// set border rect
		SetRect(&borderRect[i], menuButton[i].getRect().left, menuButton[i].getRect().top, 
			menuButton[i].getRect().right+t, menuButton[i].getRect().bottom+t);
	}
	for(int i=0; i<IGMU_BUTCNT; ++i)
	{
		// in game menu buttons
		gameButton[i].initialize(L"images/button.png", V2DF(SCREEN_WIDTH*0.5, 200+(i*100)),
			0.0f, 1.0f, true);

		// set border rect
		SetRect(&borderRect[i], gameButton[i].getRect().left, gameButton[i].getRect().top, 
			gameButton[i].getRect().right+t, gameButton[i].getRect().bottom+t);
	}
}

void UserInterface::update()
{
	switch(uis)
	{
	case NONE:
		break;
	case MENU:
		for(int i=0; i<MENU_BUTCNT; ++i) {
			// check if buttons clicked
			if(menuButton[i].hovered() && pInput->mouseButtonDown(0)) {
				if(!mbdown[i]) {
					mbdown[i] = true;
					DbgPrintf("Button [%d] clicked\n", i);
				}
			} else {
				if(mbdown[i]) {
					mbdown[i] = false;
					DbgPrintf("Button [%d] released\n", i);
				}
			}
		}
		if( pInput->mouseButtonDown(0)
			&& menuButton[0].hovered() )
		{
			setGEState(IGMENU);
			uis = IGMENU;
		}
		// exit
		if(menuButton[2].hovered() && pInput->mouseButtonDown(0))
		{
			PostQuitMessage(0);
		}
		break;
	case IGMENU:
		// set border rect
		for(int i=0; i<IGMU_BUTCNT; ++i) {
			// check if buttons clicked
			if(gameButton[i].hovered() && pInput->mouseButtonDown(0)) {
				if(!mbdown[i]) {
					mbdown[i] = true;
					DbgPrintf("Button [%d] clicked\n", i);
				}
			} else {
				if(mbdown[i]) {
					mbdown[i] = false;
					DbgPrintf("Button [%d] released\n", i);
				}
			}
		}
		// resume game
		if(menuButton[0].hovered() && pInput->mouseButtonDown(0))
		{
			resume = true;
		}
		// exit
		if(menuButton[1].hovered() && pInput->mouseButtonDown(0))
		{
			PostQuitMessage(0);
		}
		break;
	}
}

void UserInterface::render()
{ 
	switch(uis)
	{
	case NONE:
		break;
	case MENU:
		for(int i=0; i<MENU_BUTCNT; ++i)
		{
			if(menuButton[i].hovered())
				border.draw(borderRect[i], menuButton[i].getPos(), 0.0, 1.0f, true);
			menuButton[i].draw();
		}
		break;
	case IGMENU:
		//window.draw();
		for(int i=0; i<IGMU_BUTCNT; ++i)
		{
			if(gameButton[i].hovered())
				border.draw(borderRect[i], gameButton[i].getPos(), 0.0, 1.0f, true);
			gameButton[i].draw();
		}
		break;
	}
}

#define WriteButtonText( text, index) {					\
	sprintf_s(buffer, text);							\
	pEngine->writeTextCenter(buffer, temp[index]);		\
}														\

void UserInterface::drawText()
{
	switch(uis)
	{
	case MENU:
		char buffer[100];
		RECT temp[MENU_BUTCNT];
		// set texture bounds
		for(int i=0; i<MENU_BUTCNT; ++i)
		{
			temp[i].top = menuButton[i].getPos().y-(menuButton[i].getHeight()*0.5f);
			temp[i].bottom = menuButton[i].getPos().y+(menuButton[i].getHeight()*0.5f);
			temp[i].left = menuButton[i].getPos().x-(menuButton[i].getWidth()*0.5f);
			temp[i].right = menuButton[i].getPos().x+(menuButton[i].getWidth()*0.5f);
		}
		WriteButtonText("Play", 0);
		WriteButtonText("Options", 1);
		WriteButtonText("Exit", 2);
		break;
	case IGMENU:
		RECT temp2[IGMU_BUTCNT];
		// set texture bounds
		for(int i=0; i<IGMU_BUTCNT; ++i)
		{
			temp[i].top = menuButton[i].getPos().y-(menuButton[i].getHeight()*0.5f);
			temp[i].bottom = menuButton[i].getPos().y+(menuButton[i].getHeight()*0.5f);
			temp[i].left = menuButton[i].getPos().x-(menuButton[i].getWidth()*0.5f);
			temp[i].right = menuButton[i].getPos().x+(menuButton[i].getWidth()*0.5f);
		}
		WriteButtonText("Resume", 0);
		WriteButtonText("Exit", 1);
		break;
	case NONE:
		break;
	}
}

int UserInterface::getState()
{
	switch(uis)
	{
	case MENU:	return 0; break;
	case IGMENU: return 1; break;
	case NONE:	return 2; break;
	}
}

void UserInterface::release()
{

}